using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Hosting;
using BIZ;
using System.Net;
using System.Text;

namespace SinhNhat
{
    /// <summary>
    /// TextVirtualPathProvider is used to serve requests for VirtualFile1.aspx and VirtualFile2.aspx
    /// Any other requests will be redirected to ASP.Net directly without processing
    /// </summary>
    public class TextVirtualPathProvider : VirtualPathProvider
    {
        //private UrlBIZ urlBIZ;

        public TextVirtualPathProvider()
        {
            //urlBIZ = new UrlBIZ();
            //UrlBIZ.GetAllUrl();
        }

        /// <summary>
        /// special method - this is the starting point
        /// </summary>
        public static void AppInitialize()
        {
            //register the virtual path provider with the ASP.Net compilation system
            //this will generate a different execution path for the specified files
            //you can also write the same code inside Application_Start event in the Global.asax file
            
            HostingEnvironment.RegisterVirtualPathProvider(new TextVirtualPathProvider());
        }

        /// <summary>
        /// ASP.Net queries this method to check if the file requested exists or not
        /// </summary>
        /// <param name="virtualPath"></param>
        /// <returns></returns>
        public override bool FileExists(string virtualPath)
        {
            //if any of these two files then fool the user by saying the file exists (which is not correct in reality)

            if (virtualPath.Length < 5)
            {
                return Previous.FileExists(virtualPath);
            }
            string tail = virtualPath.Substring(virtualPath.Length - 5);

            if (!tail.Equals(".aspx"))
            {
                return Previous.FileExists(virtualPath);
            }

            PathType pathType = UrlBIZ.CheckPathExist(virtualPath);
            if (pathType == PathType.PhysicalPath)
            {
                return Previous.FileExists(virtualPath);
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// ASP.Net requests the virtual file. So we need to give a VirtualFile instance back
        /// </summary>
        /// <param name="virtualPath"></param>
        /// <returns></returns>
        public override VirtualFile GetFile(string virtualPath)
        {
            //if any of the two files, then give the processed file
            if (virtualPath.Length < 5)
            {
                return Previous.GetFile(virtualPath);
            }
            string tail = virtualPath.Substring(virtualPath.Length - 5);

            if (!tail.Equals(".aspx"))
            {
                return Previous.GetFile(virtualPath);
            }

            PathType pathType = UrlBIZ.CheckPathExist(virtualPath);

            if (pathType == PathType.PhysicalPath)
            {
                return Previous.GetFile(virtualPath);
            }
            else
            {
                return new TextFile(virtualPath, this);
            }
        }
    }

    /// <summary>
    /// This class returns the ASP.Net markup for the requested virtual file.
    /// </summary>
    public class TextFile : VirtualFile
    {
        private UrlBIZ urlBIZ;
        private string _path;
        private TextVirtualPathProvider _provider;
        /// <summary>
        /// constructor to initialize the member values of the local instance and of the base class
        /// </summary>
        /// <param name="virtualPath"></param>
        /// <param name="provider"></param>
        public TextFile(string virtualPath, TextVirtualPathProvider provider)
            : base(virtualPath)
        {
            _path = virtualPath.ToLower();
            _provider = provider;
            urlBIZ = new UrlBIZ();
        }

        /// <summary>
        /// This will return the ASP.Net markup in any of the stream format.
        /// </summary>
        /// <returns></returns>
        public override Stream Open()
        {
            StreamReader reader = null;
            Stream stream = new MemoryStream();

            try
            {
                StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
                PathType pathType = UrlBIZ.CheckPathExist(_path);
                Guid id = Guid.Empty; ;
                string line;

                switch (pathType)
                {
                    case PathType.CategoryPath:
                        reader = new StreamReader(HttpContext.Current.Server.MapPath("~/Category.aspx"));
                        id = urlBIZ.GetCategoryID(_path);
                        break;
                    case PathType.ArticlePath:
                        reader = new StreamReader(HttpContext.Current.Server.MapPath("~/Article.aspx"));
                        id = urlBIZ.GetArticleID(_path);
                        break;
                    case PathType.ProductPath:
                        reader = new StreamReader(HttpContext.Current.Server.MapPath("~/Product.aspx"));
                        id = urlBIZ.GetProductID(_path);
                        break;
                    case PathType.ServicePath:
                        reader = new StreamReader(HttpContext.Current.Server.MapPath("~/Service.aspx"));
                        id = urlBIZ.GetServiceID(_path);
                        break;
                    case PathType.CategoryServicePath:
                        reader = new StreamReader(HttpContext.Current.Server.MapPath("~/ServiceCategory.aspx"));
                        id = urlBIZ.GetCategoryServiceID(_path);
                        break;
                }

                line = reader.ReadLine();
                line = string.Format(line, id);
                writer.Write(line);
                line = reader.ReadLine();

                do
                {
                    writer.Write(line);
                } while ((line = reader.ReadLine()) != null);

                //while ((line = reader.ReadLine()) != null)
                //{
                //    line = string.Format(line, id);
                //    writer.Write(line);
                //}

                writer.Flush();
                stream.Seek(0, SeekOrigin.Begin);
                return stream;
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
        }

        private string AddTail(Guid id)
        {
            string tail = "<asp:Content ID=\"Content1\" ContentPlaceHolderID=\"head\" runat=\"server\">";
            tail += string.Format("<asp:HiddenField ID=\"hdID\" runat=\"server\" Value=\"{0}\" />", id);
            tail += "</asp:Content>";

            return tail;
        }
    }
}
    